#include "stdafx.h"
#include "Pref.h"
#include <strsafe.h>
#include "commondefine.h"

CPref::CPref()
{
}

CPref::~CPref()
{
}

void CPref::SetSharedPref(LPCTSTR szSharedPrefName, LPCTSTR szValue, LPCTSTR szSectionName, BOOL bEncrypt)
{
	TCHAR	KeyStr[1024];
	HKEY	hRegKey	= NULL;
	DWORD	dwDisposition;
	int		nSize	= _tcslen((TCHAR*)szValue) * sizeof(TCHAR);
	
	StringCchPrintf(KeyStr, 1024,_T("Software\\%s\\%s\\%s"), _T("SK Communications"), _T("Messenger"), szSectionName);

	if(RegCreateKeyEx(HKEY_CURRENT_USER, KeyStr, 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &hRegKey, &dwDisposition) != ERROR_SUCCESS)
	{
		if(hRegKey)
		{
			RegCloseKey(hRegKey);
		}
		return;
	}
	ASSERT(nSize >= 0);

	RegSetValueEx(hRegKey, szSharedPrefName, 0, REG_SZ, (LPBYTE)szValue, nSize);
	RegCloseKey(hRegKey);
}


CString CPref::GetSharedPref(LPCTSTR szSharedPrefName, LPCTSTR szSectionName, BOOL bEncrypted)
{
	CString strRet;
	int nMaxSize = 512*2;
	int nReadSize;
	BOOL bResult;
	
	TCHAR* pBuffer = new TCHAR[nMaxSize];

	if(!pBuffer)
		return _T("");

	memset(pBuffer, 0, nMaxSize * sizeof(TCHAR));
	nReadSize = nMaxSize;

	bResult = GetSharedPref( szSharedPrefName, pBuffer, &nReadSize, szSectionName, bEncrypted);

	if(nReadSize <= 0)
	{
		delete[] pBuffer;
		return _T("");
	}
	if(!bResult && nReadSize > nMaxSize)
	{
		nMaxSize = nReadSize;

		delete[] pBuffer;
		pBuffer = NULL;

		pBuffer = new TCHAR[nMaxSize];
		memset(pBuffer, 0, nMaxSize * sizeof(TCHAR));

		bResult = GetSharedPref( szSharedPrefName, pBuffer, &nReadSize, szSectionName, bEncrypted );
	}
	strRet = CString(pBuffer);

	if(pBuffer)
		delete[] pBuffer;
	
	return strRet;
}


BOOL CPref::GetSharedPref(LPCTSTR szSharedPrefName, LPTSTR szBuff, int * pcbBuffSize, LPCTSTR szSectionName, BOOL bEncrypted)
{
	STREAM* pBuffer		= (STREAM*)(szBuff);
	int		nBuffSize	= *pcbBuffSize * sizeof(TCHAR);
	DWORD	dwBuffSize	= (DWORD)nBuffSize;
	TCHAR	KeyStr[1024];
	HKEY	hRegKey		= NULL;

	memset(szBuff, 0, nBuffSize);

	StringCchPrintf(KeyStr, 1024, _T("Software\\%s\\%s\\%s"), _T("SK Communications"), _T("Messenger"), szSectionName);
	
	if(RegOpenKeyEx(HKEY_CURRENT_USER, KeyStr, 0, KEY_QUERY_VALUE, &hRegKey) != ERROR_SUCCESS)
	{
		*pcbBuffSize = 0;
		return FALSE;
	}
	LONG lResult = RegQueryValueEx(hRegKey, szSharedPrefName, 0, 0, (LPBYTE)pBuffer, (LPDWORD)&dwBuffSize);

	RegCloseKey(hRegKey);
	
	ASSERT(nBuffSize >= dwBuffSize);

	if(ERROR_MORE_DATA == lResult)
	{
		ASSERT(nBuffSize < dwBuffSize);
		return FALSE;
	}
	else if(ERROR_SUCCESS != lResult)
	{
		*pcbBuffSize = 0;
		return FALSE;
	}
	*pcbBuffSize = dwBuffSize / sizeof(TCHAR);

	return TRUE;
}

void CPref::DeleteRegistryKey( LPCTSTR szCompany, LPCTSTR szAppName, LPCTSTR szSection,
								    LPCTSTR szDataName)
{
	TCHAR KeyStr[1024];
	HKEY hRegKey = NULL;

	StringCchPrintf(KeyStr, 1024,_T("Software\\%s\\%s\\%s"), szCompany, szAppName, szSection);

	DWORD dwDisposition;

	if(RegCreateKeyEx(HKEY_CURRENT_USER, KeyStr, 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_WRITE, NULL, &hRegKey, &dwDisposition) != ERROR_SUCCESS)
	{
		if(hRegKey)
		{
			RegCloseKey(hRegKey);
		}
		return;
	}
	RegDeleteValue(hRegKey,szDataName);
	RegCloseKey(hRegKey);
}